<?php

/**
 * @author gbfun <gbfun@163.com>  2018-06-10 16:03
 * http://www.wuweiyou.com
 */

class db_table_member extends wwy_db_table
{
    protected $_name = 'member_member';
    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    public function find($id)
    {
        $read_db = $this->_get_read_db();
    
        $sql = '
            SELECT id, mobile, password, `default_address_id`, last_login_time, last_login_ip, login_count, create_time, update_time, status 
            FROM ' . $this->_full_name . ' AS m 
            WHERE id = ?;
        ';
        $row = $read_db->fetch_row($sql, $id);
    
        return $row;
    }
    
    public function fetch_row($where)
    {
        $read_db = $this->_get_read_db();
        
        $where      = $this->get_where($where);
        $where_str  = $where['where_str'];
        $where_bind = $where['where_bind'];
    
        $sql = '
            SELECT id, mobile, password, `default_address_id`, last_login_time, last_login_ip, login_count, create_time, update_time, status 
            FROM ' . $this->_full_name . ' AS m 
            ' . $where_str . '
        ';
        $row = $read_db->fetch_row($sql, $where_bind);
    
        return $row;
    }
    
    public function fetch_all($where = null, $order = null, $count = null, $offset = null)
    {
        $read_db = $this->_get_read_db();
    
        $where      = $this->get_where($where);
        $where_str  = $where['where_str'];
        $where_bind = $where['where_bind'];
    
        $order_str = '';
        if($order !== null){
            if(!is_array($order)){
                $order = array($order);
            }
            $order_str = 'ORDER BY ' . implode(', ', $order) . ' ';
        }
    
        $sql = '
            SELECT m.id AS id, mobile, password, `default_address_id`, last_login_time, last_login_ip, login_count, 
                create_time, update_time, status 
            FROM ' . $this->_full_name . ' AS m  
            ' . $where_str . '
            ' . $order_str . '
        ';
    
        if($count !== null){
            $sql = $read_db->limit($sql, $count, $offset);
        }
        //var_dump($sql); exit();
    
        $rows = $read_db->fetch_all($sql, $where_bind);
    
        return $rows;
    } 
    
    public function get_where($where)
    {
        if(isset($where['where_str'])){
            return $where;
        }
    
        $read_db = $this->_get_read_db();
    
        $where_str = '';
        $where_bind = array();        
        if(isset($where['ids'])){
            $ids = $read_db->quote($where['ids']);
            $where_str .= 'm.id in (' . $ids . ') ';
        }
        
        if(isset($where['mobile'])){
            if($where_str != ''){
                $where_str .= 'And ';
            }
            $where_str    .= 'mobile = ? ';
            $where_bind[]  = $where['mobile'];
        }
        if(isset($where['status'])){
            if($where_str != ''){
                $where_str .= 'And ';
            }
            $where_str    .= 'status = ? ';
            $where_bind[]  = $where['status'];
        }
        
        if(isset($where['id'])){
            if($where_str != ''){
                $where_str .= 'AND ';
            }
            $where_str    .= 'm.id = ? ';
            $where_bind[]  = $where['id'];
        }
        if(isset($where['keywords'])){
            if($where_str != ''){
                $where_str .= 'AND ';
            }
            $where_str    .= '(mobile LIKE ?) ';
            $where_bind[]  = '%' . $where['keywords'] . '%';
        }
    
        if($where_str != ''){
            $where_str = 'WHERE ' . $where_str;
        }
    
        $where = array('where_str' => $where_str, 'where_bind' => $where_bind);
    
        return $where;
    }
    
    public function get_list($where = null, $order = null, $options = array())
    {
        $where     = $this->get_where($where);
        $count     = $this->get_count($where);
        $page_data = $this->get_page_data($count, $options);
    
        $limit  = $page_data['page_item_count'];
        $offset = $page_data['start_offset'];
        $list = $this->fetch_list($where, $order, $limit, $offset);
    
        $list  = array('list' => $list, 'page_data' => $page_data);
         
        return $list;
    }
    
    public function get_count($where)
    {
        $read_db = $this->_get_read_db();
    
        $where      = $this->get_where($where);
        $where_str  = $where['where_str'];
        $where_bind = $where['where_bind'];
    
        $sql = '
            SELECT COUNT(`id`) AS count
            FROM ' . $this->_full_name . ' AS m 
            ' . $where_str . '
        ';
        $row = $read_db->fetch_row($sql, $where_bind);
    
        if(!empty($row['count'])){
            $count = intval($row['count']);
        }else{
            $count = 0;
        }
    
        return $count;
    }
    
    public function fetch_list($where = null, $order = null, $count = null, $offset = null)
    {
        $list = $this->fetch_all($where, $order, $count, $offset);
    
        return $list;
    }
    
    public function mobile_add($member_info)
    {
        $write_db = $this->_get_write_db();
    
        $sql = '
            INSERT INTO ' . $this->_full_name . '
                (
                    `mobile`, `password`, `create_time`, `update_time`, `status` 
                )
            VALUES
                (
                    ?, ?, ?, ?, ? 
                )
        ';
    
        $bind = array(
            $member_info['mobile'],
            $member_info['password'],
            $member_info['create_time'],
            $member_info['update_time'],
            $member_info['status'],            
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        if($result){
            $result = $write_db->last_insert_id();
        }
    
        return $result;
    }    
    
    public function update_member_status($member_info)
    {
        $write_db = $this->_get_write_db();
    
        $sql = '
            UPDATE ' . $this->_full_name . '
            SET
                `status` = ?,
                `update_time` = ?

            WHERE `id` = ?
        ';
    
        $bind = array(
            $member_info['status'],
            $member_info['update_time'],
            
            $member_info['id'],
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
    
        return $result;
    }
    
    
    public function update_member_login_count($member_info)
    {
        $write_db = $this->_get_write_db();
    
        $sql = '
            UPDATE ' . $this->_full_name . '
            SET
                `last_login_time` = ?,
                `last_login_ip` = ?,
                `login_count` = ?,
                `update_time` = ?
    
            WHERE `id` = ?
        ';
    
        $bind = array(
            $member_info['last_login_time'],
            $member_info['last_login_ip'],
            $member_info['login_count'],
            $member_info['update_time'],
    
            $member_info['id'],
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
    
        return $result;
    }
    
    public function update_member_password($member_info)
    {
        $write_db = $this->_get_write_db();
        
        //邮件重置密码，也可以【激活账号】
        $status_sql = '';
        if( (!empty($member_info['status'])) && ($member_info['status'] == 1) ){
            $status_sql = '`status` = "1", ';
        }
    
        $sql = '
            UPDATE ' . $this->_full_name . '
            SET
                `password` = ?, 
                ' . $status_sql . ' 
                `update_time` = ? 
    
            WHERE `id` = ?
        ';
    
        $bind = array(
            $member_info['password'],
            $member_info['update_time'],
    
            $member_info['id'],
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
    
        return $result;
    }
    
    
    
    public function update_default_address($member_info)
    {
        $write_db = $this->_get_write_db();
    
        $sql = '
            UPDATE ' . $this->_full_name . '
            SET
                `default_address_id` = ?,
                `update_time` = ?
    
            WHERE `id` = ?
        ';
    
        $bind = array(
            $member_info['default_address_id'],
            $member_info['update_time'],
    
            $member_info['id'],
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
    
        return $result;
    }
}

